package srdb.migration.dbhandler.redwood;


import srdb.migration.common.MTKConnection;

import java.io.FileInputStream;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Hashtable;
import java.util.Properties;

public class RedwoodConnection extends MTKConnection {
    private static String redwoodDriver = "oracle.jdbc.driver.OracleDriver";


    public RedwoodConnection(String dbURL, String dbUser, String dbPassword) throws Exception {
        super(dbURL, dbUser, dbPassword);
//        try {
        Class.forName(redwoodDriver);
        this.conn = DriverManager.getConnection(dbURL, dbUser, dbPassword);

        loadDBServerParams();
//        } catch (SQLException se) {
//            throw new Exception(se.getMessage());
//        }
    }


    public RedwoodConnection() {
    }

    public RedwoodConnection(String propertiesFile)

            throws Exception {
        super(propertiesFile);
        try {
            Properties ini = new Properties();
            ini.load(new FileInputStream(propertiesFile));

            this.dbURL = ini.getProperty("RedwoodConnection");
            this.dbUser = ini.getProperty("RedwoodUser");
            this.dbPassword = ini.getProperty("RedwoodPassword");

            Class.forName(redwoodDriver);

            this.conn = DriverManager.getConnection(this.dbURL, this.dbUser, this.dbPassword);

            loadDBServerParams();
        } catch (SQLException se) {
            throw new Exception(se.getMessage());
        }
    }

    private void loadDBServerParams()
            throws SQLException {
        String paramQuery = "SELECT parameter, value FROM v$nls_parameters WHERE parameter IN ('NLS_CHARACTERSET','NLS_NCHAR_CHARACTERSET','NLS_LANGUAGE','NLS_TERRITORY')";

        Statement stmt = null;
        ResultSet rs = null;
        try {
            stmt = this.conn.createStatement();
            rs = stmt.executeQuery(paramQuery);

            while (rs.next()) {
                if (this.dbServerParams == null) {
                    this.dbServerParams = new Hashtable();
                }

                this.dbServerParams.put(rs.getString("parameter"), rs.getString("value"));
            }
        } finally {
            if (stmt != null) {
                stmt.close();
            }

            if (rs != null)
                rs.close();
        }
    }

    public static void main(String[] args) {
        String url = "jdbc:oracle:thin:@10.168.250.42:1521:ora11";
        String user = "system";
        String password = "123456";
        try {
            RedwoodConnection redwoodConnection = new RedwoodConnection(url, user, password);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public  boolean preConnectDB(String[] dbInfo) throws Exception {
        boolean connMsg = true;
        String url = "jdbc:oracle:thin:@" + dbInfo[0] + ":" + dbInfo[1] + ":" + dbInfo[2];//"jdbc:oracle:thin:@10.168.250.42:1521:ora11";

        String user = dbInfo[3];
        String password = dbInfo[4];
        RedwoodConnection redwoodConnection = new RedwoodConnection(url, user, password);

        return connMsg;
    }
}

/* 

 * 
 */